//
//  ViewController.m
//  SKPAD
//
//  Created by admin@amonline.com.cn on 15/5/4.
//  Copyright (c) 2015年 amo. All rights reserved.
//

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //初始化几个变量
    countRow = 8;
    screenSize = [[UIScreen mainScreen] bounds].size;
    cornerRadius = 8;
    space = screenSize.height/256.0;
    
    TableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, screenSize.height, screenSize.width) style:UITableViewStylePlain];

    TableView.transform = CGAffineTransformMakeRotation(M_PI/2);
    TableView.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);

    [TableView setBackgroundColor:[UIColor blackColor]];
    //清楚分割线左边留有15像素的空白
    if ([TableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [TableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([TableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [TableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    //隐藏分隔线
    TableView.separatorStyle = NO;
    [TableView setDelegate:self];		    [TableView setDataSource:self];
    //[self.view addSubview:TableView];
    
    //进度条
    _verticalThreeTone = [[VerticalThreeToneControl alloc]  initWithFrame:CGRectMake(10, 10, 15, 50)];
    _verticalThreeTone.lineWidth = 3.0f;
    _verticalThreeTone.minimumValue = 0.f;
    _verticalThreeTone.maximumValue = 100;
    //[self.view addSubview:_verticalThreeTone];
    
    //进度条
    //_horizontalTwoTone = [[HorizontalTwoToneControl alloc]  initWithFrame:self.knobPlaceholdertwo.frame];
    //_horizontalTwoTone.lineWidth = 2.0f;
    //_horizontalTwoTone.minimumValue = 0.f;
    //_horizontalTwoTone.maximumValue = 100;
    //[self.knobPlaceholdertwo addSubview:_horizontalTwoTone];
    
    //旋钮
    //CGRect rect;
    //rect.origin.x = self.view.frame.size.width/2;
    //rect.origin.y = self.view.frame.size.height/2;
    //rect.size.width = 150;
    //rect.size.height = 150;
    //_testView = [[KnobControl alloc] initWithFrame: rect];
    //[self.view addSubview:_testView];
    
    //slider

    //CGRect rect;
    //rect.origin.x = 0;
    //rect.origin.y = 0;
    //rect.size.width = screenSize.width;
    //rect.size.height = screenSize.height;
    //_frequencyControl = [[FrequencyControl alloc] initWithFrame:rect];
    //[self.view addSubview:_frequencyControl];

    //resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)上，左，下，右 用做图片拉伸
    //CGRect rect;
    //rect.origin.x = 0;
    //rect.origin.y = self.view.frame.size.width/4;
    //rect.size.width = 500;
    //rect.size.height = 30;
    //self.volumeSlide = [[UISlider alloc]initWithFrame:rect];
    //UIImage *MaxImg=[UIImage imageNamed:@"dropdown_socket_blue_left.png"];
    //UIImage *MaxImg_Fin=[MaxImg TransformtoSize:CGSizeMake(12, 12)];
    //UIImage *MaxImg2=[UIImage imageNamed:@"dropdown_socket_black_right.png"];
    //UIImage *MaxImg_Fin2=[MaxImg2 TransformtoSize:CGSizeMake(12, 12)];
    
    //[_volumeSlide setMinimumTrackImage:[MaxImg_Fin resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 0)] forState:UIControlStateNormal];
    //[_volumeSlide setMaximumTrackImage:[MaxImg_Fin2 resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 5)] forState:UIControlStateNormal];
    // Set up our motion updates
    //[self setupMotionDetection];
    //[_volumeSlide setThumbImage: [UIImage imageNamed:@"fader_button.png"] forState:UIControlStateNormal];
    
    //[self.view addSubview:_volumeSlide];
    //self.standardSlider = [[CustomSlider alloc] initWithFrame:rect];
    //[self.view addSubview:_standardSlider];
    
    //_ceshi = [[CustomSlider alloc] initWithFrame:CGRectMake(50, 50, 100, 13)];
    //[self.view addSubview:_ceshi];
    
    //_testBar = [[TwoColorProgressBar alloc] initWithFrame:CGRectMake(150, 150, 30, 550)];
    //[self.view addSubview:_testBar];
    
    //_testIrr = [[IrregularSixShape alloc] initWithFrame:CGRectMake(10, 10, 500, 250)];
    //[self.view addSubview:_testIrr];
    //_testTwoWaySlider = [[TwoWayMiniSlider alloc] initWithFrame:CGRectMake(10, 10, 300, 49)];
    //[self.view addSubview:_testTwoWaySlider];
    
    //_testThreeColor = [[ThreeColorProgressBar alloc] initWithFrame:CGRectMake(200, 300, 20, 300)];
    //[self.view addSubview:_testThreeColor];
    
    //_testSingleColor = [[SingleColorProgressBar alloc] initWithFrame:CGRectMake(200, 200, 80, 9)];
    //[self.view addSubview:_testSingleColor];
    
    _testDoubleTwoColor = [[DoubleTwoColorProgressBar alloc] initWithFrame:CGRectMake(200, 50, 30, 304)];
    [self.view addSubview:_testDoubleTwoColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
          toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
//行数
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return countRow;
}
//每行宽度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return screenSize.width/8;
}

-(void)setExtraCellLineHidden:(UITableView *)tableView
{
    UIView * view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //清楚分割线左边留有15像素的空白
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)])
    {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
}
//初始化每列信息
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"Cell";
    UITableViewCell * cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    //设置透明
    cell.backgroundColor=[UIColor clearColor];//关键语句
    
    //添加背景图片
    UIImageView * backImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"out_lr_ch_bg.png"]];
    backImageView.frame = CGRectMake(space, space, screenSize.height - 2.0*space, screenSize.width/countRow - space);
    backImageView.layer.cornerRadius = cornerRadius;
    backImageView.layer.masksToBounds = YES;
    
    // 53 95 75 208 33 95---height --566 -----screenSize.height = 762;
    CGFloat totalHeight = 569;
    CGFloat jg = backImageView.frame.size.width * 2.0/totalHeight;
    //--------
    UIImage *inputStageBgImg=[UIImage imageNamed:@"ch_input_stage_bg.png"];
    //图片尺寸
    CGFloat inputStageBgWidth = screenSize.width/countRow - space*2;
    CGFloat inputStageBgHeight = screenSize.height * 53/totalHeight - space;
    UIImage *inputStageBgFin=[inputStageBgImg TransformtoSize:CGSizeMake(inputStageBgWidth, inputStageBgHeight)];
    UIImageView * inputStageBg = [[UIImageView alloc] initWithImage:inputStageBgFin];
    
    inputStageBg.transform = CGAffineTransformMakeRotation(-M_PI/2.0);
    inputStageBg.frame = CGRectMake(jg, space/2.0, inputStageBgHeight, inputStageBgWidth);
    
    [backImageView.layer insertSublayer:inputStageBg.layer atIndex:0];
    
    jg += inputStageBgHeight + backImageView.frame.size.width * 2.0/totalHeight;
    //-----------
    UIImage *eqBgImg=[UIImage imageNamed:@"ch_eq_bg.png"];
    CGFloat eqBgWidth = screenSize.width/countRow - space*2;
    CGFloat eqBgHeight = screenSize.height * 95/totalHeight - space;
    
    UIImage *eqBgFin=[eqBgImg TransformtoSize:CGSizeMake(eqBgWidth, eqBgHeight)];
    UIImageView * eqBg = [[UIImageView alloc] initWithImage:eqBgFin];
    eqBg.transform = CGAffineTransformMakeRotation(-M_PI/2.0);
    eqBg.frame = CGRectMake(jg, space/2.0, eqBgHeight, eqBgWidth);
    [backImageView.layer insertSublayer:eqBg.layer atIndex:0];
    
    jg += eqBgHeight + backImageView.frame.size.width * 2.0/totalHeight;
    //-----------
    UIImage *dyBgImg=[UIImage imageNamed:@"ch_dy_bg.png"];
    CGFloat dyBgWidth = screenSize.width/countRow - space*2;
    CGFloat dyBgHeight = screenSize.height * 75/totalHeight - space;
    
    UIImage *dyBgFin=[dyBgImg TransformtoSize:CGSizeMake(dyBgWidth, dyBgHeight)];
    UIImageView * dyBg = [[UIImageView alloc] initWithImage:dyBgFin];
    dyBg.transform = CGAffineTransformMakeRotation(-M_PI/2.0);
    dyBg.frame = CGRectMake(jg, space/2.0, dyBgHeight, dyBgWidth);
    [backImageView.layer insertSublayer:dyBg.layer atIndex:0];

    jg += dyBgHeight + backImageView.frame.size.width * 2.0/totalHeight;
    //-------------------
    UIImage *busBgImg=[UIImage imageNamed:@"ch_bus_bg.png"];
    CGFloat busBgWidth = screenSize.width/countRow - space*2;
    CGFloat busBgHeight = screenSize.height * 208/totalHeight - space;
    
    UIImage *busBgFin=[busBgImg TransformtoSize:CGSizeMake(busBgWidth, busBgHeight)];
    UIImageView * busBg = [[UIImageView alloc] initWithImage:busBgFin];
    busBg.transform = CGAffineTransformMakeRotation(-M_PI/2.0);
    busBg.frame = CGRectMake(jg, space/2.0, busBgHeight, busBgWidth);
    [backImageView.layer insertSublayer:busBg.layer atIndex:0];
    
    jg += busBgHeight + backImageView.frame.size.width * 2.0/totalHeight;
    
    //---------------
    UIImage *squarensButtonImg=[UIImage imageNamed:@"button_squarens_nosel_blue.png"];
    CGFloat squarensButtonWidth = screenSize.width/countRow - space*2;
    CGFloat squarensButtonHeight = screenSize.height * 33/totalHeight - space;
    
    UIImage *squarensButtonFin=[squarensButtonImg TransformtoSize:CGSizeMake(squarensButtonWidth, squarensButtonHeight)];
    UIImageView * squarensButton = [[UIImageView alloc] initWithImage:squarensButtonFin];
    squarensButton.transform = CGAffineTransformMakeRotation(-M_PI/2.0);
    squarensButton.frame = CGRectMake(jg, space/2.0,squarensButtonHeight, squarensButtonWidth);
    [backImageView.layer insertSublayer:squarensButton.layer atIndex:0];
    
    jg += squarensButtonHeight + backImageView.frame.size.width * 2.0/totalHeight;
    //---------------
    
    //进度条
    CGRect rect;
    rect.origin.x = jg + (backImageView.frame.size.width - jg)/2.0;
    rect.origin.y = 0;
    rect.size.width = backImageView.frame.size.width/countRow/12;
    rect.size.height = screenSize.height * 80/totalHeight - space;
   // UIView * threeView = [[UIView alloc] initWithFrame:rect];
    NSLog(@"%f-%f-%f-%f", rect.origin.x,rect.origin.y, rect.size.width,rect.size.height);
    _ch1VerticalThreeTone = [[VerticalThreeToneControl alloc]  initWithFrame:rect];
    _ch1VerticalThreeTone.transform = CGAffineTransformMakeRotation(-M_PI/2.0);
    _ch1VerticalThreeTone.lineWidth = 1.f;
    _ch1VerticalThreeTone.minimumValue = 0.f;
    _ch1VerticalThreeTone.maximumValue = 100.0;
    _ch1VerticalThreeTone.value = 100.0;
    [backImageView.layer insertSublayer:_ch1VerticalThreeTone.layer atIndex:1];
    //[threeView addSubview:_ch1VerticalThreeTone];
    //[backImageView addSubview:threeView];
    [cell addSubview: backImageView];
    //修改选中时的颜色为无色
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    return cell;
}

//table的cell选中事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(prevIndexPath != nil)
    {
        [self tableView:tableView defaultCellStyle:prevIndexPath];
    }
    
    [self tableView:tableView selectCellStyle:indexPath];
    prevIndexPath = indexPath;
}

//
-(void)tableView:(UITableView *)tableView defaultCellStyle:(NSIndexPath *)indexPath
{
    //获取选中行
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    //添加背景图片
    UIImageView * backImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"out_lr_ch_bg.png"]];
    backImageView.frame = CGRectMake(5, 5, screenSize.height - 5, screenSize.width/8 - 5);
    backImageView.layer.cornerRadius = cornerRadius;
    backImageView.layer.masksToBounds = YES;
    [cell addSubview: backImageView];
    
    //设置所有的cell按钮
    UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame = CGRectMake(0, 0, 71, 80);
    btn1.transform = CGAffineTransformMakeRotation(-M_PI/2);
    [btn1 setTitle:@"测试" forState:UIControlStateNormal];
    [cell addSubview:btn1];
    
    UIButton * btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn2.frame = CGRectMake(90, 0, 71, 80);
    [btn2 setTitle:@"测试2" forState:UIControlStateNormal];
    [cell addSubview:btn2];
}

-(void)tableView:(UITableView *)tableView selectCellStyle:(NSIndexPath *)indexPath
{
    //获取选中行
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    //初始背景图片设置
    UIImageView * selectionBackImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"out_lr_ch_selected_bg.png"]];
    selectionBackImageView.frame = CGRectMake(5, 5, screenSize.height - 5, screenSize.width/8 - 5);
    selectionBackImageView.layer.cornerRadius = cornerRadius;
    selectionBackImageView.layer.masksToBounds = YES;
    [cell addSubview: selectionBackImageView];
    
    UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame = CGRectMake(0, 0, 71, 80);
    btn1.transform = CGAffineTransformMakeRotation(-M_PI/2);
    [btn1 setTitle:@"测试" forState:UIControlStateNormal];
    [cell addSubview:btn1];
    
    UIButton * btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn2.frame = CGRectMake(90, 0, 71, 80);
    [btn2 setTitle:@"测试2" forState:UIControlStateNormal];
    [cell addSubview:btn2];
}

- (IBAction)radambutton:(id)sender {

    CGFloat randomValue1 = (arc4random() % 101)/100.f;
    //[_verticalThreeTone setValue:randomValue1 * 100.f];
    //[_horizontalTwoTone setValue:randomValue1 * 100.f];
    //[_testView setValue:randomValue1 animated:YES];
    [self.valueSlider setValue:randomValue1 animated:YES];
    _testBar.value = randomValue1;
    // Then set it on the two controls
}

- (IBAction)slider:(id)sender {
    //NSLog(@"slider = %i", (int)(self.valueSlider.value * 100));
    //_testBar.value = self.valueSlider.value;
    //[_testSingleColor setMyRealValue:self.valueSlider.value];
    [_testDoubleTwoColor setRealRightValue:self.valueSlider.value * 100];
    //[_testDoubleTwoColor setRealLeftValue:self.valueSlider.value * 100];
}
- (IBAction)slider2:(id)sender {
    //NSLog(@"slider = %i", (int)(self.valueSlider.value * 100));
    //_testBar.value = self.valueSlider.value;
    //[_testSingleColor setMyRealValue:self.valueSlider.value];
    //[_testDoubleTwoColor setRealRightValue:self.valueSlider.value * 100];
    [_testDoubleTwoColor setRealLeftValue:self.valueSlider2.value * 100];
}
@end
